package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.myapplication.javabean.Courses;
import com.example.myapplication.javabean.User;

import java.util.ArrayList;
import java.util.List;

public class MySQLiteOpenHelper1 extends SQLiteOpenHelper {
//    创建user表
    private static final String CREATE_USER = "create table user(name varchar(32) UNIQUE NOT NULL,password varchar(32) ,studentName varchar(32) UNIQUE,id INTEGER PRIMARY KEY AUTOINCREMENT,className varchar(32));";
    private static final String DB_NAME = "mySQL6";
    //创建课程表
    private static final String CREATE_COURSES="CREATE TABLE courses (\n" +
            "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
            "    courseName VARCHAR(255) UNIQUE NOT NULL,\n" +
            "    teacherName VARCHAR(255) NOT NULL,\n" +
            "    time VARCHAR(255) NOT NULL,\n" +
            "    location VARCHAR(255) NOT NULL\n" +
            ");";


    public MySQLiteOpenHelper1(Context context) {
        super(context, DB_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db1) {
        db1.execSQL(CREATE_USER);
        db1.execSQL(CREATE_COURSES);
        //初始化courses数据
        db1.execSQL("INSERT INTO courses (courseName, teacherName, time, location) \n" +
                "VALUES ('计算机科学导论  ', '张老师', '周一 10:00-12:00', '教学楼101室');");
        db1.execSQL("INSERT INTO courses (courseName, teacherName, time, location) \n" +
                "VALUES ('数据结构与算法  ', '李老师', '周三 14:00-16:00', '教学楼202室');");
        db1.execSQL("INSERT INTO courses (courseName, teacherName, time, location) \n" +
                "VALUES ('计算机原理     ', '李老师', '周三 15:00-16:00', '教学楼202室');");

        db1.execSQL("insert into user(name,password,studentName,id,className) values('a5dmin','2123','张三',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('us1er1','21231','王大嘴',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('man','2232','李二麻',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('s145un','2123','张老三',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('and51eoic','12332','孔老二',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('and53roid','3122','王老五',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('adse','223442','王小周',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('dat134a','22346','周可怜',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('shds12a','2352','马小帅',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('shd32sa','232352','打小蛋',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('shd435sa','2352','蛋蛋',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('shd1242a','23352','嘴嘴',null,'计算机科学与技术三班')");
        db1.execSQL("insert into user(name,password,studentName,id,className) values('admin','123','孙膑',null,'计算机科学与技术三班')");


    }
    @Override
    public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) {

    }

    //注册添加数据
    public long register(Courses courses) {
        SQLiteDatabase db1 =getReadableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("courseName", courses.getCourseName());
        cv.put("teacherName", courses.getTeacherName());
        cv.put("time",courses.getTime());
        cv.put("location",courses.getLocation());
        long coursers = db1.insert("courses", null, cv);
        return coursers;
    }
    //注册添加数据
    public long register(User user) {
        SQLiteDatabase db1 =getReadableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("name", user.getName());
        cv.put("password", user.getPassword());
        cv.put("studentName",user.getStudentName());
        cv.put("className",user.getClassName());
        long users = db1.insert("user", null, cv);
        Log.d("MySQLiteOpenHelper1", "Insert result: " + users);
        return users;
    }



    //登录条件查询数据
    public boolean login(String name,String password)
    {
        SQLiteDatabase db1 = getWritableDatabase();
        boolean result=false;
        //表明，要
        Cursor users = db1.query("user", null, "name= ?", new String[]{name}, null, null, null);
        if (users!=null)
        {
            while (users.moveToNext())
            {
                String password1 = users.getString(1);
                 result = password1.equals(password);
                 return result;


            }
        }
return false;
    }

//获取user表中的数据 查询全部数据
    @SuppressLint("Range")
    public List<User> getAllUsers() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        List<User> users = new ArrayList<>();
        try {
            cursor = db.rawQuery("SELECT * FROM user", null);
            if (cursor.moveToFirst()) {
                do {
                    User user = new User();
                    user.setName(cursor.getString(cursor.getColumnIndex("name")));
                    user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                    user.setStudentName(cursor.getString(cursor.getColumnIndex("studentName")));
                    user.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    user.setClassName(cursor.getString(cursor.getColumnIndex("className")));
                    users.add(user);
                } while (cursor.moveToNext());
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return users;
    }
    // 获取courses表中的数据
    @SuppressLint("Range")
    public List<Courses> getAllCourses() { // 修改方法名和返回类型
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null; // 更改变量名为cursor以保持一致性
        List<Courses> coursesList = new ArrayList<>(); // 更改变量名以匹配返回类型

        try {
            cursor = db.rawQuery("SELECT * FROM courses", null);
            if (cursor.moveToFirst()) {
                do {
                    Courses course = new Courses(); // 创建Courses对象
                    course.setCourseName(cursor.getString(cursor.getColumnIndex("courseName")));
                    course.setTeacherName(cursor.getString(cursor.getColumnIndex("teacherName")));
                    course.setTime(cursor.getString(cursor.getColumnIndex("time")));
                    course.setLocation(cursor.getString(cursor.getColumnIndex("location")));

                    coursesList.add(course); // 添加到列表
                } while (cursor.moveToNext());
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return coursesList; // 返回正确的Courses列表
    }
    // 更新用户名和密码
    public void updatePassword(String username, String newPassword) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("password", newPassword);
        db.update("user", contentValues, "name=?", new String[]{username});
        db.close();
    }


}
